package liuds.music.model;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.widget.Toast;

import liuds.music.utils.Utils;

/**
 * Created by Administrator on 2018/5/7.
 */

public class NetWorkStateReceiver extends BroadcastReceiver {

    private OnNetWorkChangeListener onNetWorkChangeListener;

    public NetWorkStateReceiver(OnNetWorkChangeListener onNetWorkChangeListener){
        this.onNetWorkChangeListener = onNetWorkChangeListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            //获取ConnectivityManager对象对应的NetworkInfo对象
            //获取WIFI连接的信息
            NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //获取移动数据连接的信息
            NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if (!wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
                onNetWorkChangeListener.onDisconnect();
            }else if(wifiNetworkInfo.isConnected()){
                onNetWorkChangeListener.onConnect();
            }else if(dataNetworkInfo.isConnected()){
                onNetWorkChangeListener.onConnect();
            }
    }

    public interface OnNetWorkChangeListener{
        void onDisconnect();
        void onConnect();
    }
}
